//
//  ChangeLoginPassVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/3.
//  Copyright © 2020 tym. All rights reserved.
//

#import "ChangeOpenLockPassVC.h"
#import "XMPayCodeView.h"
#import "FamilyService.h"
#import "IQKeyboardManager.h"
#import "UIView+ZFFrame.h"
@interface ChangeOpenLockPassVC ()

@property(nonatomic,weak) XMPayCodeView *onePayCodeView;
@property(nonatomic,weak) XMPayCodeView *twoPayCodeView;

@property(nonatomic,strong) FamilyService *services;
@end

@implementation ChangeOpenLockPassVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:@"修改密码"];
    
     self.automaticallyAdjustsScrollViewInsets = NO;
     [[IQKeyboardManager sharedManager] setEnable:NO];
    UILabel *oneLab = [TYMLabel lb_text:@"请输入新的6位开锁密码" font:Font_Regular(15) color:rgba(53, 54, 70)];
    oneLab.frame = CGRectMake(15, 59, 180, 21);
    [self.view addSubview:oneLab];
    
    XMPayCodeView *payView = [[XMPayCodeView alloc]initWithFrame:CGRectMake(0, 95, self.view.bounds.size.width, 60)];
    payView.secureTextEntry = YES;
    [payView setPayBlock:^(NSString *payCode) {
        if (payCode.length == 6) {
            [self.twoPayCodeView becomeKeyBoardFirstResponder];
        }
    }];
    [self.view addSubview:payView];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [payView becomeKeyBoardFirstResponder];
    });
    self.onePayCodeView = payView;
    
    UILabel *towLab = [TYMLabel lb_text:@"请确认新的开锁密码" font:Font_Regular(15) color:rgba(53, 54, 70)];
    towLab.frame = CGRectMake(15, self.onePayCodeView.zf_bottom+55, 190, 21);
    [self.view addSubview:towLab];
    
    XMPayCodeView *payTow = [[XMPayCodeView alloc]initWithFrame:CGRectMake(0,self.onePayCodeView.zf_bottom+91, ScreenWidth, 60)];
    payTow.secureTextEntry = YES;
    payTow.endEditingOnFinished = YES;
    [self.view addSubview:payTow];
    [payTow setPayBlock:^(NSString *payCode) {

        [self setPasswordAction:payCode];
    }];
    self.twoPayCodeView = payTow;
}

-(FamilyService*)services{
    if(_services == nil){
        _services = [[FamilyService alloc] init];
    }
    return _services;
}

-(void)setPasswordAction:(NSString*)pawStr{
    
    if (![self.onePayCodeView.payCode isEqualToString:pawStr]) {
        [WHToast showMessage:@"两次密码不一致，请重新输入" duration:2 finishHandler:nil];
    
        return;
    }
    
    TYMWeak(self);
    [self.services getUpdateoperationpwd:pawStr completion:^(LMJBaseResponse * _Nonnull responObjec) {
        if (responObjec.message) {
            [WHToast showMessage:responObjec.message duration:2 finishHandler:nil];
        }else{
            [weakself.navigationController popViewControllerAnimated:YES];
        }
    }];
}

@end
